/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Localityelement;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Waterbody;
import entityBeans.Stateprovince;
import entityBeans.Site;
import entityBeans.Municipality;
import entityBeans.Locality;
import entityBeans.Islandgroup;
import entityBeans.Island;
import entityBeans.Habitat;
import entityBeans.Georeferenceverificationstatus;
import entityBeans.County;
import entityBeans.Country;
import entityBeans.Continent;
import entityBeans.Georeferencesource;
import java.util.ArrayList;
import java.util.List;
import entityBeans.Georeferencedby;
import entityBeans.Specimen;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class LocalityelementJpaController {

    public LocalityelementJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Localityelement localityelement) {
        if (localityelement.getGeoreferencesourceList() == null) {
            localityelement.setGeoreferencesourceList(new ArrayList<Georeferencesource>());
        }
        if (localityelement.getGeoreferencedbyList() == null) {
            localityelement.setGeoreferencedbyList(new ArrayList<Georeferencedby>());
        }
        if (localityelement.getSpecimenList() == null) {
            localityelement.setSpecimenList(new ArrayList<Specimen>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Waterbody waterbody = localityelement.getWaterbody();
            if (waterbody != null) {
                waterbody = em.getReference(waterbody.getClass(), waterbody.getIdwaterbody());
                localityelement.setWaterbody(waterbody);
            }
            Stateprovince stateprovince = localityelement.getStateprovince();
            if (stateprovince != null) {
                stateprovince = em.getReference(stateprovince.getClass(), stateprovince.getIdstateprovince());
                localityelement.setStateprovince(stateprovince);
            }
            Site site = localityelement.getSite();
            if (site != null) {
                site = em.getReference(site.getClass(), site.getIdsite());
                localityelement.setSite(site);
            }
            Municipality municipality = localityelement.getMunicipality();
            if (municipality != null) {
                municipality = em.getReference(municipality.getClass(), municipality.getIdmunicipality());
                localityelement.setMunicipality(municipality);
            }
            Locality locality = localityelement.getLocality();
            if (locality != null) {
                locality = em.getReference(locality.getClass(), locality.getIdlocality());
                localityelement.setLocality(locality);
            }
            Islandgroup islandgroup = localityelement.getIslandgroup();
            if (islandgroup != null) {
                islandgroup = em.getReference(islandgroup.getClass(), islandgroup.getIdislandgroup());
                localityelement.setIslandgroup(islandgroup);
            }
            Island island = localityelement.getIsland();
            if (island != null) {
                island = em.getReference(island.getClass(), island.getIdisland());
                localityelement.setIsland(island);
            }
            Habitat habitat = localityelement.getHabitat();
            if (habitat != null) {
                habitat = em.getReference(habitat.getClass(), habitat.getIdhabitat());
                localityelement.setHabitat(habitat);
            }
            Georeferenceverificationstatus georeferenceverificationstatus = localityelement.getGeoreferenceverificationstatus();
            if (georeferenceverificationstatus != null) {
                georeferenceverificationstatus = em.getReference(georeferenceverificationstatus.getClass(), georeferenceverificationstatus.getIdgeoreferenceverificationstatus());
                localityelement.setGeoreferenceverificationstatus(georeferenceverificationstatus);
            }
            County county = localityelement.getCounty();
            if (county != null) {
                county = em.getReference(county.getClass(), county.getIdcounty());
                localityelement.setCounty(county);
            }
            Country country = localityelement.getCountry();
            if (country != null) {
                country = em.getReference(country.getClass(), country.getIdcountry());
                localityelement.setCountry(country);
            }
            Continent continent = localityelement.getContinent();
            if (continent != null) {
                continent = em.getReference(continent.getClass(), continent.getIdcontinent());
                localityelement.setContinent(continent);
            }
            List<Georeferencesource> attachedGeoreferencesourceList = new ArrayList<Georeferencesource>();
            for (Georeferencesource georeferencesourceListGeoreferencesourceToAttach : localityelement.getGeoreferencesourceList()) {
                georeferencesourceListGeoreferencesourceToAttach = em.getReference(georeferencesourceListGeoreferencesourceToAttach.getClass(), georeferencesourceListGeoreferencesourceToAttach.getIdgeoreferencesource());
                attachedGeoreferencesourceList.add(georeferencesourceListGeoreferencesourceToAttach);
            }
            localityelement.setGeoreferencesourceList(attachedGeoreferencesourceList);
            List<Georeferencedby> attachedGeoreferencedbyList = new ArrayList<Georeferencedby>();
            for (Georeferencedby georeferencedbyListGeoreferencedbyToAttach : localityelement.getGeoreferencedbyList()) {
                georeferencedbyListGeoreferencedbyToAttach = em.getReference(georeferencedbyListGeoreferencedbyToAttach.getClass(), georeferencedbyListGeoreferencedbyToAttach.getIdgeoreferencedby());
                attachedGeoreferencedbyList.add(georeferencedbyListGeoreferencedbyToAttach);
            }
            localityelement.setGeoreferencedbyList(attachedGeoreferencedbyList);
            List<Specimen> attachedSpecimenList = new ArrayList<Specimen>();
            for (Specimen specimenListSpecimenToAttach : localityelement.getSpecimenList()) {
                specimenListSpecimenToAttach = em.getReference(specimenListSpecimenToAttach.getClass(), specimenListSpecimenToAttach.getIdspecimen());
                attachedSpecimenList.add(specimenListSpecimenToAttach);
            }
            localityelement.setSpecimenList(attachedSpecimenList);
            em.persist(localityelement);
            if (waterbody != null) {
                waterbody.getLocalityelementList().add(localityelement);
                waterbody = em.merge(waterbody);
            }
            if (stateprovince != null) {
                stateprovince.getLocalityelementList().add(localityelement);
                stateprovince = em.merge(stateprovince);
            }
            if (site != null) {
                site.getLocalityelementList().add(localityelement);
                site = em.merge(site);
            }
            if (municipality != null) {
                municipality.getLocalityelementList().add(localityelement);
                municipality = em.merge(municipality);
            }
            if (locality != null) {
                locality.getLocalityelementList().add(localityelement);
                locality = em.merge(locality);
            }
            if (islandgroup != null) {
                islandgroup.getLocalityelementList().add(localityelement);
                islandgroup = em.merge(islandgroup);
            }
            if (island != null) {
                island.getLocalityelementList().add(localityelement);
                island = em.merge(island);
            }
            if (habitat != null) {
                habitat.getLocalityelementList().add(localityelement);
                habitat = em.merge(habitat);
            }
            if (georeferenceverificationstatus != null) {
                georeferenceverificationstatus.getLocalityelementList().add(localityelement);
                georeferenceverificationstatus = em.merge(georeferenceverificationstatus);
            }
            if (county != null) {
                county.getLocalityelementList().add(localityelement);
                county = em.merge(county);
            }
            if (country != null) {
                country.getLocalityelementList().add(localityelement);
                country = em.merge(country);
            }
            if (continent != null) {
                continent.getLocalityelementList().add(localityelement);
                continent = em.merge(continent);
            }
            for (Georeferencesource georeferencesourceListGeoreferencesource : localityelement.getGeoreferencesourceList()) {
                georeferencesourceListGeoreferencesource.getLocalityelementList().add(localityelement);
                georeferencesourceListGeoreferencesource = em.merge(georeferencesourceListGeoreferencesource);
            }
            for (Georeferencedby georeferencedbyListGeoreferencedby : localityelement.getGeoreferencedbyList()) {
                georeferencedbyListGeoreferencedby.getLocalityelementList().add(localityelement);
                georeferencedbyListGeoreferencedby = em.merge(georeferencedbyListGeoreferencedby);
            }
            for (Specimen specimenListSpecimen : localityelement.getSpecimenList()) {
                Localityelement oldLocalityelementOfSpecimenListSpecimen = specimenListSpecimen.getLocalityelement();
                specimenListSpecimen.setLocalityelement(localityelement);
                specimenListSpecimen = em.merge(specimenListSpecimen);
                if (oldLocalityelementOfSpecimenListSpecimen != null) {
                    oldLocalityelementOfSpecimenListSpecimen.getSpecimenList().remove(specimenListSpecimen);
                    oldLocalityelementOfSpecimenListSpecimen = em.merge(oldLocalityelementOfSpecimenListSpecimen);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Localityelement localityelement) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Localityelement persistentLocalityelement = em.find(Localityelement.class, localityelement.getIdlocalityelement());
            Waterbody waterbodyOld = persistentLocalityelement.getWaterbody();
            Waterbody waterbodyNew = localityelement.getWaterbody();
            Stateprovince stateprovinceOld = persistentLocalityelement.getStateprovince();
            Stateprovince stateprovinceNew = localityelement.getStateprovince();
            Site siteOld = persistentLocalityelement.getSite();
            Site siteNew = localityelement.getSite();
            Municipality municipalityOld = persistentLocalityelement.getMunicipality();
            Municipality municipalityNew = localityelement.getMunicipality();
            Locality localityOld = persistentLocalityelement.getLocality();
            Locality localityNew = localityelement.getLocality();
            Islandgroup islandgroupOld = persistentLocalityelement.getIslandgroup();
            Islandgroup islandgroupNew = localityelement.getIslandgroup();
            Island islandOld = persistentLocalityelement.getIsland();
            Island islandNew = localityelement.getIsland();
            Habitat habitatOld = persistentLocalityelement.getHabitat();
            Habitat habitatNew = localityelement.getHabitat();
            Georeferenceverificationstatus georeferenceverificationstatusOld = persistentLocalityelement.getGeoreferenceverificationstatus();
            Georeferenceverificationstatus georeferenceverificationstatusNew = localityelement.getGeoreferenceverificationstatus();
            County countyOld = persistentLocalityelement.getCounty();
            County countyNew = localityelement.getCounty();
            Country countryOld = persistentLocalityelement.getCountry();
            Country countryNew = localityelement.getCountry();
            Continent continentOld = persistentLocalityelement.getContinent();
            Continent continentNew = localityelement.getContinent();
            List<Georeferencesource> georeferencesourceListOld = persistentLocalityelement.getGeoreferencesourceList();
            List<Georeferencesource> georeferencesourceListNew = localityelement.getGeoreferencesourceList();
            List<Georeferencedby> georeferencedbyListOld = persistentLocalityelement.getGeoreferencedbyList();
            List<Georeferencedby> georeferencedbyListNew = localityelement.getGeoreferencedbyList();
            List<Specimen> specimenListOld = new ArrayList<Specimen>(); //persistentLocalityelement.getSpecimenList();
            List<Specimen> specimenListNew = new ArrayList<Specimen>(); //localityelement.getSpecimenList();
            if (waterbodyNew != null) {
                waterbodyNew = em.getReference(waterbodyNew.getClass(), waterbodyNew.getIdwaterbody());
                localityelement.setWaterbody(waterbodyNew);
            }
            if (stateprovinceNew != null) {
                stateprovinceNew = em.getReference(stateprovinceNew.getClass(), stateprovinceNew.getIdstateprovince());
                localityelement.setStateprovince(stateprovinceNew);
            }
            if (siteNew != null) {
                siteNew = em.getReference(siteNew.getClass(), siteNew.getIdsite());
                localityelement.setSite(siteNew);
            }
            if (municipalityNew != null) {
                municipalityNew = em.getReference(municipalityNew.getClass(), municipalityNew.getIdmunicipality());
                localityelement.setMunicipality(municipalityNew);
            }
            if (localityNew != null) {
                localityNew = em.getReference(localityNew.getClass(), localityNew.getIdlocality());
                localityelement.setLocality(localityNew);
            }
            if (islandgroupNew != null) {
                islandgroupNew = em.getReference(islandgroupNew.getClass(), islandgroupNew.getIdislandgroup());
                localityelement.setIslandgroup(islandgroupNew);
            }
            if (islandNew != null) {
                islandNew = em.getReference(islandNew.getClass(), islandNew.getIdisland());
                localityelement.setIsland(islandNew);
            }
            if (habitatNew != null) {
                habitatNew = em.getReference(habitatNew.getClass(), habitatNew.getIdhabitat());
                localityelement.setHabitat(habitatNew);
            }
            if (georeferenceverificationstatusNew != null) {
                georeferenceverificationstatusNew = em.getReference(georeferenceverificationstatusNew.getClass(), georeferenceverificationstatusNew.getIdgeoreferenceverificationstatus());
                localityelement.setGeoreferenceverificationstatus(georeferenceverificationstatusNew);
            }
            if (countyNew != null) {
                countyNew = em.getReference(countyNew.getClass(), countyNew.getIdcounty());
                localityelement.setCounty(countyNew);
            }
            if (countryNew != null) {
                countryNew = em.getReference(countryNew.getClass(), countryNew.getIdcountry());
                localityelement.setCountry(countryNew);
            }
            if (continentNew != null) {
                continentNew = em.getReference(continentNew.getClass(), continentNew.getIdcontinent());
                localityelement.setContinent(continentNew);
            }
            List<Georeferencesource> attachedGeoreferencesourceListNew = new ArrayList<Georeferencesource>();
            for (Georeferencesource georeferencesourceListNewGeoreferencesourceToAttach : georeferencesourceListNew) {
                georeferencesourceListNewGeoreferencesourceToAttach = em.getReference(georeferencesourceListNewGeoreferencesourceToAttach.getClass(), georeferencesourceListNewGeoreferencesourceToAttach.getIdgeoreferencesource());
                attachedGeoreferencesourceListNew.add(georeferencesourceListNewGeoreferencesourceToAttach);
            }
            georeferencesourceListNew = attachedGeoreferencesourceListNew;
            localityelement.setGeoreferencesourceList(georeferencesourceListNew);
            List<Georeferencedby> attachedGeoreferencedbyListNew = new ArrayList<Georeferencedby>();
            for (Georeferencedby georeferencedbyListNewGeoreferencedbyToAttach : georeferencedbyListNew) {
                georeferencedbyListNewGeoreferencedbyToAttach = em.getReference(georeferencedbyListNewGeoreferencedbyToAttach.getClass(), georeferencedbyListNewGeoreferencedbyToAttach.getIdgeoreferencedby());
                attachedGeoreferencedbyListNew.add(georeferencedbyListNewGeoreferencedbyToAttach);
            }
            georeferencedbyListNew = attachedGeoreferencedbyListNew;
            localityelement.setGeoreferencedbyList(georeferencedbyListNew);
            List<Specimen> attachedSpecimenListNew = new ArrayList<Specimen>();
            for (Specimen specimenListNewSpecimenToAttach : specimenListNew) {
                specimenListNewSpecimenToAttach = em.getReference(specimenListNewSpecimenToAttach.getClass(), specimenListNewSpecimenToAttach.getIdspecimen());
                attachedSpecimenListNew.add(specimenListNewSpecimenToAttach);
            }
            specimenListNew = attachedSpecimenListNew;
            localityelement.setSpecimenList(specimenListNew);
            localityelement = em.merge(localityelement);
            if (waterbodyOld != null && !waterbodyOld.equals(waterbodyNew)) {
                waterbodyOld.getLocalityelementList().remove(localityelement);
                waterbodyOld = em.merge(waterbodyOld);
            }
            if (waterbodyNew != null && !waterbodyNew.equals(waterbodyOld)) {
                waterbodyNew.getLocalityelementList().add(localityelement);
                waterbodyNew = em.merge(waterbodyNew);
            }
            if (stateprovinceOld != null && !stateprovinceOld.equals(stateprovinceNew)) {
                stateprovinceOld.getLocalityelementList().remove(localityelement);
                stateprovinceOld = em.merge(stateprovinceOld);
            }
            if (stateprovinceNew != null && !stateprovinceNew.equals(stateprovinceOld)) {
                stateprovinceNew.getLocalityelementList().add(localityelement);
                stateprovinceNew = em.merge(stateprovinceNew);
            }
            if (siteOld != null && !siteOld.equals(siteNew)) {
                siteOld.getLocalityelementList().remove(localityelement);
                siteOld = em.merge(siteOld);
            }
            if (siteNew != null && !siteNew.equals(siteOld)) {
                siteNew.getLocalityelementList().add(localityelement);
                siteNew = em.merge(siteNew);
            }
            if (municipalityOld != null && !municipalityOld.equals(municipalityNew)) {
                municipalityOld.getLocalityelementList().remove(localityelement);
                municipalityOld = em.merge(municipalityOld);
            }
            if (municipalityNew != null && !municipalityNew.equals(municipalityOld)) {
                municipalityNew.getLocalityelementList().add(localityelement);
                municipalityNew = em.merge(municipalityNew);
            }
            if (localityOld != null && !localityOld.equals(localityNew)) {
                localityOld.getLocalityelementList().remove(localityelement);
                localityOld = em.merge(localityOld);
            }
            if (localityNew != null && !localityNew.equals(localityOld)) {
                localityNew.getLocalityelementList().add(localityelement);
                localityNew = em.merge(localityNew);
            }
            if (islandgroupOld != null && !islandgroupOld.equals(islandgroupNew)) {
                islandgroupOld.getLocalityelementList().remove(localityelement);
                islandgroupOld = em.merge(islandgroupOld);
            }
            if (islandgroupNew != null && !islandgroupNew.equals(islandgroupOld)) {
                islandgroupNew.getLocalityelementList().add(localityelement);
                islandgroupNew = em.merge(islandgroupNew);
            }
            if (islandOld != null && !islandOld.equals(islandNew)) {
                islandOld.getLocalityelementList().remove(localityelement);
                islandOld = em.merge(islandOld);
            }
            if (islandNew != null && !islandNew.equals(islandOld)) {
                islandNew.getLocalityelementList().add(localityelement);
                islandNew = em.merge(islandNew);
            }
            if (habitatOld != null && !habitatOld.equals(habitatNew)) {
                habitatOld.getLocalityelementList().remove(localityelement);
                habitatOld = em.merge(habitatOld);
            }
            if (habitatNew != null && !habitatNew.equals(habitatOld)) {
                habitatNew.getLocalityelementList().add(localityelement);
                habitatNew = em.merge(habitatNew);
            }
            if (georeferenceverificationstatusOld != null && !georeferenceverificationstatusOld.equals(georeferenceverificationstatusNew)) {
                georeferenceverificationstatusOld.getLocalityelementList().remove(localityelement);
                georeferenceverificationstatusOld = em.merge(georeferenceverificationstatusOld);
            }
            if (georeferenceverificationstatusNew != null && !georeferenceverificationstatusNew.equals(georeferenceverificationstatusOld)) {
                georeferenceverificationstatusNew.getLocalityelementList().add(localityelement);
                georeferenceverificationstatusNew = em.merge(georeferenceverificationstatusNew);
            }
            if (countyOld != null && !countyOld.equals(countyNew)) {
                countyOld.getLocalityelementList().remove(localityelement);
                countyOld = em.merge(countyOld);
            }
            if (countyNew != null && !countyNew.equals(countyOld)) {
                countyNew.getLocalityelementList().add(localityelement);
                countyNew = em.merge(countyNew);
            }
            if (countryOld != null && !countryOld.equals(countryNew)) {
                countryOld.getLocalityelementList().remove(localityelement);
                countryOld = em.merge(countryOld);
            }
            if (countryNew != null && !countryNew.equals(countryOld)) {
                countryNew.getLocalityelementList().add(localityelement);
                countryNew = em.merge(countryNew);
            }
            if (continentOld != null && !continentOld.equals(continentNew)) {
                continentOld.getLocalityelementList().remove(localityelement);
                continentOld = em.merge(continentOld);
            }
            if (continentNew != null && !continentNew.equals(continentOld)) {
                continentNew.getLocalityelementList().add(localityelement);
                continentNew = em.merge(continentNew);
            }
            for (Georeferencesource georeferencesourceListOldGeoreferencesource : georeferencesourceListOld) {
                if (!georeferencesourceListNew.contains(georeferencesourceListOldGeoreferencesource)) {
                    georeferencesourceListOldGeoreferencesource.getLocalityelementList().remove(localityelement);
                    georeferencesourceListOldGeoreferencesource = em.merge(georeferencesourceListOldGeoreferencesource);
                }
            }
            for (Georeferencesource georeferencesourceListNewGeoreferencesource : georeferencesourceListNew) {
                if (!georeferencesourceListOld.contains(georeferencesourceListNewGeoreferencesource)) {
                    georeferencesourceListNewGeoreferencesource.getLocalityelementList().add(localityelement);
                    georeferencesourceListNewGeoreferencesource = em.merge(georeferencesourceListNewGeoreferencesource);
                }
            }
            for (Georeferencedby georeferencedbyListOldGeoreferencedby : georeferencedbyListOld) {
                if (!georeferencedbyListNew.contains(georeferencedbyListOldGeoreferencedby)) {
                    georeferencedbyListOldGeoreferencedby.getLocalityelementList().remove(localityelement);
                    georeferencedbyListOldGeoreferencedby = em.merge(georeferencedbyListOldGeoreferencedby);
                }
            }
            for (Georeferencedby georeferencedbyListNewGeoreferencedby : georeferencedbyListNew) {
                if (!georeferencedbyListOld.contains(georeferencedbyListNewGeoreferencedby)) {
                    georeferencedbyListNewGeoreferencedby.getLocalityelementList().add(localityelement);
                    georeferencedbyListNewGeoreferencedby = em.merge(georeferencedbyListNewGeoreferencedby);
                }
            }
            for (Specimen specimenListOldSpecimen : specimenListOld) {
                if (!specimenListNew.contains(specimenListOldSpecimen)) {
                    specimenListOldSpecimen.setLocalityelement(null);
                    specimenListOldSpecimen = em.merge(specimenListOldSpecimen);
                }
            }
            for (Specimen specimenListNewSpecimen : specimenListNew) {
                if (!specimenListOld.contains(specimenListNewSpecimen)) {
                    Localityelement oldLocalityelementOfSpecimenListNewSpecimen = specimenListNewSpecimen.getLocalityelement();
                    specimenListNewSpecimen.setLocalityelement(localityelement);
                    specimenListNewSpecimen = em.merge(specimenListNewSpecimen);
                    if (oldLocalityelementOfSpecimenListNewSpecimen != null && !oldLocalityelementOfSpecimenListNewSpecimen.equals(localityelement)) {
                        oldLocalityelementOfSpecimenListNewSpecimen.getSpecimenList().remove(specimenListNewSpecimen);
                        oldLocalityelementOfSpecimenListNewSpecimen = em.merge(oldLocalityelementOfSpecimenListNewSpecimen);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = localityelement.getIdlocalityelement();
                if (findLocalityelement(id) == null) {
                    throw new NonexistentEntityException("The localityelement with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Localityelement localityelement;
            try {
                localityelement = em.getReference(Localityelement.class, id);
                localityelement.getIdlocalityelement();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The localityelement with id " + id + " no longer exists.", enfe);
            }
            Waterbody waterbody = localityelement.getWaterbody();
            if (waterbody != null) {
                waterbody.getLocalityelementList().remove(localityelement);
                waterbody = em.merge(waterbody);
            }
            Stateprovince stateprovince = localityelement.getStateprovince();
            if (stateprovince != null) {
                stateprovince.getLocalityelementList().remove(localityelement);
                stateprovince = em.merge(stateprovince);
            }
            Site site = localityelement.getSite();
            if (site != null) {
                site.getLocalityelementList().remove(localityelement);
                site = em.merge(site);
            }
            Municipality municipality = localityelement.getMunicipality();
            if (municipality != null) {
                municipality.getLocalityelementList().remove(localityelement);
                municipality = em.merge(municipality);
            }
            Locality locality = localityelement.getLocality();
            if (locality != null) {
                locality.getLocalityelementList().remove(localityelement);
                locality = em.merge(locality);
            }
            Islandgroup islandgroup = localityelement.getIslandgroup();
            if (islandgroup != null) {
                islandgroup.getLocalityelementList().remove(localityelement);
                islandgroup = em.merge(islandgroup);
            }
            Island island = localityelement.getIsland();
            if (island != null) {
                island.getLocalityelementList().remove(localityelement);
                island = em.merge(island);
            }
            Habitat habitat = localityelement.getHabitat();
            if (habitat != null) {
                habitat.getLocalityelementList().remove(localityelement);
                habitat = em.merge(habitat);
            }
            Georeferenceverificationstatus georeferenceverificationstatus = localityelement.getGeoreferenceverificationstatus();
            if (georeferenceverificationstatus != null) {
                georeferenceverificationstatus.getLocalityelementList().remove(localityelement);
                georeferenceverificationstatus = em.merge(georeferenceverificationstatus);
            }
            County county = localityelement.getCounty();
            if (county != null) {
                county.getLocalityelementList().remove(localityelement);
                county = em.merge(county);
            }
            Country country = localityelement.getCountry();
            if (country != null) {
                country.getLocalityelementList().remove(localityelement);
                country = em.merge(country);
            }
            Continent continent = localityelement.getContinent();
            if (continent != null) {
                continent.getLocalityelementList().remove(localityelement);
                continent = em.merge(continent);
            }
            List<Georeferencesource> georeferencesourceList = localityelement.getGeoreferencesourceList();
            for (Georeferencesource georeferencesourceListGeoreferencesource : georeferencesourceList) {
                georeferencesourceListGeoreferencesource.getLocalityelementList().remove(localityelement);
                georeferencesourceListGeoreferencesource = em.merge(georeferencesourceListGeoreferencesource);
            }
            List<Georeferencedby> georeferencedbyList = localityelement.getGeoreferencedbyList();
            for (Georeferencedby georeferencedbyListGeoreferencedby : georeferencedbyList) {
                georeferencedbyListGeoreferencedby.getLocalityelementList().remove(localityelement);
                georeferencedbyListGeoreferencedby = em.merge(georeferencedbyListGeoreferencedby);
            }
            List<Specimen> specimenList = localityelement.getSpecimenList();
            for (Specimen specimenListSpecimen : specimenList) {
                specimenListSpecimen.setLocalityelement(null);
                specimenListSpecimen = em.merge(specimenListSpecimen);
            }
            em.remove(localityelement);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Localityelement> findLocalityelementEntities() {
        return findLocalityelementEntities(true, -1, -1);
    }

    public List<Localityelement> findLocalityelementEntities(int maxResults, int firstResult) {
        return findLocalityelementEntities(false, maxResults, firstResult);
    }

    private List<Localityelement> findLocalityelementEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Localityelement.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Localityelement findLocalityelement(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Localityelement.class, id);
        } finally {
            em.close();
        }
    }

    public int getLocalityelementCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Localityelement> rt = cq.from(Localityelement.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
